﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace BISPOS
{
    
    public partial class EditProduct_SelectProductForm : DevExpress.XtraEditors.XtraForm
    {
        EditProductUserControl editProductUserControl;
        List<Product> products;
        public EditProduct_SelectProductForm(EditProductUserControl _editProductUserControl, List<Product> _products)
        {
            InitializeComponent();
            editProductUserControl = _editProductUserControl;
            products = _products;
            productListlinqServerModeSource.QueryableSource = products.AsQueryable();
        }

        private void allProductsListBoxControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.Text = "เลือกสินค้า - " + allProductsListBoxControl.SelectedItems.OfType<Product>().FirstOrDefault().BarcodeAndName;
            }
            catch { }
        }

        private void EditProduct_SelectProductForm_Shown(object sender, EventArgs e)
        {
            searchTextEdit.Focus();
        }

        private void searchTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                if (allProductsListBoxControl.SelectedIndex != 0)
                {
                    try { allProductsListBoxControl.SelectedIndex -= 1; }
                    catch { }
                }
            }
            if (e.KeyCode == Keys.Down)
            {
                try { allProductsListBoxControl.SelectedIndex += 1; }
                catch { }
            }
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    this.editProductUserControl.AddProduct(allProductsListBoxControl.SelectedItems.OfType<Product>().FirstOrDefault());
                    this.Close();
                }
                catch
                {

                }
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void searchTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                productListlinqServerModeSource.QueryableSource =  products.Where(x => x.BarcodeAndName.Contains(searchTextEdit.Text)).AsQueryable();
                productListlinqServerModeSource.Reload();
            }
            catch { }
        }


    }
}